package com.jiufen.Service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jiufen.Mapper.StudentMapper;
import com.jiufen.Pojo.PageResult;
import com.jiufen.Pojo.Student;
import com.jiufen.Pojo.StudentQueryParam;
import com.jiufen.Service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;


    /**
     * 根据条件分页查询学生信息
     * @param studentQueryParam
     * @return
     */
    @Override
    public PageResult<Student> getStudentList(StudentQueryParam studentQueryParam) {
        log.debug("分页查询学生信息参数");
        PageHelper.startPage(studentQueryParam.getPage(),studentQueryParam.getPageSize());
        List<Student> students = studentMapper.queryStudentListByPage(studentQueryParam);
        Page<Student> studentsPage = (Page<Student>) students;
        log.debug("分页查询学生信息结果{}",studentsPage);
        return new PageResult<>(studentsPage.getTotal(),studentsPage.getResult());
    }

    @Override
    public void saveStudent(Student student) {
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        student.setViolationCount(0);
        student.setViolationScore(0);
        log.debug("保存学生信息Service={]",student);
        studentMapper.insertStudent(student);
        return;
    }

    @Override
    public Student getStudentById(Integer id) {
        log.debug("Service-根据id查询学生信息={}",id);
        Student student = studentMapper.queryStudentById(id);
        log.debug("Service-根据id查询学生信息Result={}",student);
        return student;
    }

    /**
     * 4.5 修改学员
     * 4.5.1 基本信息
     * 请求路径：/students
     * 请求方式：PUT
     * 接口描述：该接口用于修改学员的数据信息
     *
     * 4.5.2 请求参数
     * 参数格式：application/json
     * 参数说明：
     * @param student
     */
    @Override
    public void updateStudent(Student student) {
        student.setUpdateTime(LocalDateTime.now());
        log.debug("修改学员信息={}",student);
        studentMapper.updateStudent(student);
        return;
    }


    /**
     * 4.2 删除学员
     * 4.2.1 基本信息
     * 请求路径：/students/{ids}
     * 请求方式：DELETE
     * 接口描述：该接口用于批量删除学员信息
     *
     * 4.2.2 请求参数
     * 参数格式：路径参数
     * 参数说明:
     * 请求参数样例：/students/1,2,3
     */
    @Override
    public Integer deleteStudent(Integer[] ids) {
        Integer deleteNumber = studentMapper.deleteStudentById(ids);
        return deleteNumber;
    }



    /**
     * 4.6 违纪处理
     * 4.6.1 基本信息
     * 请求路径：/students/violation/{id}/{score}
     * 请求方式：PUT
     * 接口描述：该接口用于修改学员的数据信息
     *
     * 4.6.2 请求参数
     * 参数格式：路径参数
     * 参数说明：
     * 暂时无法在飞书文档外展示此内容
     */
    @Override
    public void updateStudentViolation(Integer id, Integer score) {
        //查询学生信息
        Student student = studentMapper.queryStudentById(id);
        //修改违纪信息和更新日期
        student.setUpdateTime(LocalDateTime.now());
        student.setViolationCount(student.getViolationCount()+1);
        student.setViolationScore(student.getViolationScore()+score);
        log.debug("学生违纪信息更新={}",student);
        studentMapper.updateStudent(student);
        return;
    }

}
